package com.ruoyi.web.cp.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BaseService {

    public void cleanGames(String gamePath, String cleanPath, Integer hours) {
        List<String> games = FileUtil.readLines(gamePath, "utf8");
        if (hours == null || hours == 0) {
            FileUtil.clean(cleanPath);
        } else {
            long time = DateUtil.offsetHour(new Date(), hours).getTime();
            games.forEach(game -> {
                String[] split = game.split("\t");
                String str = split[2];
                long time2 = DateUtil.parse(str, "yyyy-MM-dd HH:ss").getTime();
                if (time2 < time) {
                    FileUtil.del(cleanPath + System.getProperty("file.separator") + split[0] + ".txt");
                }
            });
        }

    }
}
